{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/user/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "#要开发一个自动识别谱子的吉他模拟器，达到录入谱即可按照谱发声的效果。\n",
    "# 除了发声设备外（假设已完成），最重要的就是读谱和译谱能力了。\n",
    "# 分析其需求，整个过程大致上分可以分为两部分：\n",
    "# 根据规则翻译谱的内容；根据翻译的内容演奏。\n",
    "# 我们用一个解释器模型来完成这个功能。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class PlayContext():\n",
    "    play_text = None\n",
    "\n",
    "class Expression():\n",
    "    def interpret(self, context):\n",
    "        if len(context.play_text) == 0:\n",
    "            return\n",
    "        else:\n",
    "            play_segs=context.play_text.split(\" \")\n",
    "            for play_seg in play_segs:\n",
    "                pos=0\n",
    "                for ele in play_seg:\n",
    "                    if ele.isalpha():#检验字符串是否只由字母组成\n",
    "                        pos+=1\n",
    "                        continue\n",
    "                    break\n",
    "                play_chord = play_seg[0:pos]\n",
    "                play_value = play_seg[pos:]\n",
    "                self.execute(play_chord,play_value)\n",
    "    def execute(self,play_key,play_value):\n",
    "        pass\n",
    "\n",
    "class NormGuitar(Expression):\n",
    "    def execute(self, key, value):\n",
    "        print(\"Normal Guitar Playing--Chord:%s Play Tune:%s\"%(key,value))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normal Guitar Playing--Chord:C Play Tune:53231323\n",
      "Normal Guitar Playing--Chord:Em Play Tune:43231323\n",
      "Normal Guitar Playing--Chord:F Play Tune:43231323\n",
      "Normal Guitar Playing--Chord:G Play Tune:63231323\n"
     ]
    }
   ],
   "source": [
    "#PlayContext类为谱的内容，这里仅含一个字段，没有方法。\n",
    "# Expression即表达式，里面仅含两个方法，interpret负责转译谱，execute则负责演奏；NormGuitar类覆写execute，以吉他 的方式演奏。\n",
    "#业务场景如下：\n",
    "if __name__==\"__main__\":\n",
    "    context = PlayContext()\n",
    "    context.play_text = \"C53231323 Em43231323 F43231323 G63231323\"\n",
    "    guitar=NormGuitar()\n",
    "    guitar.interpret(context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
